package com.peanut.baby.mvp;

import android.content.Context;
import android.graphics.BitmapFactory.Options;
import android.graphics.drawable.StateListDrawable;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout.LayoutParams;

import com.peanut.baby.InitManager;
import com.peanut.baby.R;
import com.peanut.baby.model.Photo;
import com.peanut.devlibrary.adapter.HolderAdapter;
import com.peanut.devlibrary.imageloader.ImageLoader;
import com.peanut.devlibrary.util.SysUtil;

import java.util.List;


/**
 * 关心的人症状ListView的数据适配器
 * Title: SymptomListAdapter
 * Company: Taetao
 * @date 2015年5月15日
 * @author ShaoZhuRui
 */
public class PhotoGridAdapter extends HolderAdapter<Photo,PhotoGridAdapter.PhotoHolder> {
//	private MyImageLoaderListener	mListener;


	private int 					mCellSize,mImageSize;

	private StateListDrawable 		mDrawable;



	public PhotoGridAdapter(Context ctx, List<Photo> photos){
		super(ctx,photos);
		mCellSize = (InitManager.getInstance().getScreenWidth() - SysUtil.dip2px(55)) /5;
		mImageSize = mCellSize - SysUtil.dip2px(8);
		mDrawable = (StateListDrawable) ctx.getResources().getDrawable(R.drawable.selector_btn_add_photo);
		mDrawable.setBounds(0, 0, mDrawable.getIntrinsicWidth(), mDrawable.getIntrinsicHeight());


		Options options = new Options();
		options.inSampleSize = 10;
	}


	@Override
	public View buildConvertView(LayoutInflater layoutInflater, Photo photo, int position) {
		return inflate(R.layout.grid_item_add_photo);
	}

	@Override
	public PhotoHolder buildHolder(View convertView, Photo goodInfo, int position) {
		PhotoHolder holder = new PhotoHolder();
		holder.mFrame = (FrameLayout) convertView.findViewById(R.id.attach_grid_frame);
		holder.mImage = (ImageView) convertView.findViewById(R.id.attach_grid_image);
		holder.mDelete = (ImageView) convertView.findViewById(R.id.attach_grid_delete);
		resizeViews(holder.mFrame, holder.mImage);

		return holder;
	}

	@Override
	public void bindViewDatas(PhotoHolder holder, final Photo photo, final int position) {
		holder.mDelete.setVisibility(View.GONE);
		if(!TextUtils.isEmpty(photo.url)){
			ImageLoader.getInstance().loadImageView(context,photo.url, holder.mImage);

		}else{
			ImageLoader.getInstance().loadImageView(context,photo.path, holder.mImage);

		}

	}


	class PhotoHolder{
		FrameLayout mFrame;
		ImageView mImage;
		ImageView mDelete;

	}




	
	private void resizeViews(FrameLayout frame,ImageView image){
		LayoutParams params = (LayoutParams) frame.getLayoutParams();
		params.width = mCellSize;
		params.height = mCellSize;
		frame.setLayoutParams(params);
		
		FrameLayout.LayoutParams _params = (FrameLayout.LayoutParams) image.getLayoutParams();
		_params.width = mImageSize;
		_params.height = mImageSize;
		_params.gravity = Gravity.BOTTOM;
		image.setLayoutParams(_params);
		
	}

	public interface OnPhotoDeleteListener{
		void onPhotoDelete(int position);
	}



	
//	private class MyImageLoaderListener implements ImageLoadingListener{
//
//		@Override
//		public void onLoadingStarted(String imageUri, View view) {
//
//		}
//
//		@Override
//		public void onLoadingFailed(String imageUri, View view,
//				FailReason failReason) {
//		}
//
//		@Override
//		public void onLoadingComplete(String imageUri, final View view,
//				Bitmap loadedImage) {
//			if(loadedImage != null && !loadedImage.isRecycled()){
//				String tag = (String) view.getTag();
//				if(tag.equals(imageUri)){
//					((ImageView) view).setImageBitmap(loadedImage);
//				}else if(tag.equals(Photo.FLAG_ADD)){
//					((ImageView) view).setImageDrawable(mDrawable);
//				}
//			}
//
//		}
//
//		@Override
//		public void onLoadingCancelled(String imageUri, View view) {
//			// TODO Auto-generated method stub
//
//		}
//
//	}
	
}
